package com.qumitech.visualnovel.service.guild.impl;

import com.alibaba.fastjson.JSON;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.dao.app.GuildMemberDO;
import com.qumitech.visualnovel.model.dao.app.GuildCfgDO;
import com.qumitech.visualnovel.model.dao.app.Account;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.guild.GuildCfgAdminDTO;
import com.qumitech.visualnovel.service.exception.ExceptionService;
import com.qumitech.visualnovel.service.guild.GuildService;
import com.qumitech.visualnovel.datasource.app.GuildMemberDOMapper;
import com.qumitech.visualnovel.datasource.app.GuildCfgDOMapper;
import com.qumitech.visualnovel.datasource.app.AccountMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.collect.ImmutableMap;

import java.util.*;

/**
 * @className: GuildServiceImpl
 * @description:
 * @author: bzhao
 * @date: 2020/10/12 8:50 下午
 * copyright(c) 2017-2020 趣米（武汉）科技有限公司
 */
@Service
public class GuildServiceImpl implements GuildService {

    private static final String GUILD_CONFIG_URL = "/admin/guild";

    @Autowired
    private ExceptionService exceptionService;

    @Autowired
    private GuildMemberDOMapper guildMemberDOMapper;

    @Autowired
    private GuildCfgDOMapper guildCfgDOMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public BaseRsp listGuildCfg(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + GUILD_CONFIG_URL;
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    @Override
    public BaseRsp createGuild(GuildCfgAdminDTO dto) {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        dto.setOperator(activeUser.getUsername());
        String url = UrlConstant.SERVER_URL + GUILD_CONFIG_URL;
        String body = HttpInvokeUtil.httpPostJson(url, JSON.toJSONString(dto),"utf-8",null);
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    @Override
    public BaseRsp updateGuildCfg(Integer guildId, GuildCfgAdminDTO dto) {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        dto.setOperator(activeUser.getUsername());
        String url = UrlConstant.SERVER_URL + GUILD_CONFIG_URL + "/" + guildId;
        String body = HttpInvokeUtil.httpPostJson(url, JSON.toJSONString(dto),"utf-8",null);
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    @Override
    public BaseRsp deleteGuild(Integer guildId) {
        String url = UrlConstant.SERVER_URL + GUILD_CONFIG_URL + "/" + guildId;
        String body = HttpInvokeUtil.httpDelete(url, new HashMap<>());
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    @Override
    public BaseRsp listMembers(Integer memberUid, Integer guildId, Integer status,
                               String startTime, String endTime, int offset, int limit) {
        int count = guildMemberDOMapper.getCount(memberUid, guildId, status, startTime, endTime);
        List<GuildMemberDO> list = guildMemberDOMapper.getList(memberUid, guildId, status, startTime, endTime, offset, limit);
        Set<String> uidSet = new HashSet();
        for (GuildMemberDO item : list) {
            uidSet.add(String.valueOf(item.getOwnerUid()));
        }
        Map<Long, String> uidMap = getNicknameMap(uidSet);
        for (GuildMemberDO item : list) {
            item.setOwnerName(uidMap.get(item.getOwnerUid()));
        }
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", list, "count", count));
    }

    private Map<Long, String> getNicknameMap(Set<String> uidSet) {

        List<String> uidList = new ArrayList(uidSet);
        List<Account> accountList = accountMapper.getNicknameList(uidList);

        Map<Long, String> uidMap = new HashMap<>();
        for (Account item : accountList) {
            uidMap.put(item.getUid(), item.getUserName());
        }
        return uidMap;
    }

    @Override
    public BaseRsp listMemberOptionsStatus() {
        Map<String, String> list = new HashMap<String, String>();
        list.put("1", "正常");
        list.put("2", "退出中");
        list.put("3", "同意退出");
        list.put("4", "强制退出");
        list.put("5", "被移除");
        list.put("6", "被解散");
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", list, "count", list.size()));
    }

    @Override
    public BaseRsp listMemberOptionsGuild() {
        List<GuildCfgDO> list = guildCfgDOMapper.getAllGuildNames();
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", list, "count", list.size()));
    }
}
